package generator.controller.backend;

import generator.domain.AppCategory;
import generator.domain.AppInfo;
import generator.domain.BackendUser;
import generator.domain.DataDictionary;
import generator.service.AppCategoryService;
import generator.service.AppInfoService;
import generator.service.BackendUserService;
import generator.service.DataDictionaryService;
import generator.utis.Constants;
import generator.utis.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description: TODO
 * @author: 李展鹏
 * @date: 2024年05月17日10:54
 */
@Controller
@RequestMapping("/manager")
public class BackendUserController {
    @Resource
    BackendUserService backendUserService;

    @Resource
    AppInfoService appInfoService;
    @Resource
    AppCategoryService appCategoryService;
    @Resource
    DataDictionaryService dataDictionaryService;
    //跳转到手游管理登录
    @RequestMapping("/login")
    public String BackendUserLogin(){
        return "backendlogin";
    }
    //登录操作
    @RequestMapping("/toBackendUserLogin")
    public String doBackendUserLogin(@RequestParam("usercode") String userCode,
                                     @RequestParam("userpassword") String userPassword,
                                     HttpSession session, HttpServletRequest request){
        BackendUser backendUser=backendUserService.getBackendUser(userCode);
        if (backendUser==null){
            //用户不存在
            request.setAttribute("error","用户不存在");
            return "backendlogin";
        }
        //如果用户不存在接下来就开始验证密码
        if (backendUser.getUserpassword().equals(userPassword)){
            //密码正确、将用户信息存入session
            session.setAttribute("userSession",backendUser);
            return "redirect:/manager/toMain";
        }
        return "backendlogin";
    }
    //跳转到首页
    @RequestMapping("/toMain")
    private String main(HttpSession session){
        if (session.getAttribute("userSession")==null){
            return "redirect:/manager/login";
        }
        return "backend/main";
    }

    //分页
    @RequestMapping("/list")
    private String BackendUser(Model model, @RequestParam(value = "querySoftwareName",required = false) String softwareName,
                               @RequestParam(required = false)Integer status,
                               @RequestParam(value = "queryCategoryLevel1",required = false) Integer categoryLevel1,
                               @RequestParam(value = "queryCategoryLevel2",required = false) Integer categoryLevel2,
                               @RequestParam(value = "queryCategoryLevel3",required = false) Integer categoryLevel3,
                               @RequestParam(value = "queryFlatformId",required = false) Integer flatformId,
                               @RequestParam(required = false)Integer devId,
                               @RequestParam(value = "currentPageNo",required = false,defaultValue = "1") Integer currentPageNo){
        List<AppInfo> appInfoList = null;
        List<DataDictionary> dataDictionaries = null;
        List<generator.domain.AppCategory> appCategorie1 = null;
        List<generator.domain.AppCategory> appCategorie2 = null;
        List<AppCategory> appCategorie3 = null;
        try {
            int pageSize = Constants.pageSize;
            int totalCount =appInfoService.getAppInfoCount(softwareName,status,categoryLevel1,categoryLevel2,categoryLevel3,flatformId,devId);
            //总页数
            PageSupport pages = new PageSupport();
            pages.setCurrentPageNo(currentPageNo);
            pages.setPageSize(pageSize);
            pages.setTotalCount(totalCount);
            int totalPageCount = pages.getTotalPageCount();
            //控制首页和尾页
            if (currentPageNo > totalPageCount){
                currentPageNo = totalPageCount;
                throw new RuntimeException("页码不正确");
            }
            appInfoList = appInfoService.getAppInfoList(softwareName,status,categoryLevel1,categoryLevel2,categoryLevel3,flatformId,devId,currentPageNo,pageSize);
            dataDictionaries = dataDictionaryService.getDataDictionaryList();
            appCategorie1 = appCategoryService.getAppCategory1List(1);
            appCategorie2 = appCategoryService.getAppCategory1List(categoryLevel1);
            appCategorie3 = appCategoryService.getAppCategory1List(categoryLevel2);
            model.addAttribute("appInfoList",appInfoList);
            model.addAttribute("flatFormList",dataDictionaries);
            model.addAttribute("categoryLevel1List",appCategorie1);
            model.addAttribute("categoryLevel2List",appCategorie2);
            model.addAttribute("categoryLevel3List",appCategorie3);
            model.addAttribute("querySoftwareName",softwareName);
            model.addAttribute("queryCategoryLevel1",categoryLevel1);
            model.addAttribute("queryCategoryLevel2",categoryLevel2);
            model.addAttribute("queryCategoryLevel3",categoryLevel3);
            model.addAttribute("queryFlatformId",flatformId);
            model.addAttribute("devId",devId);
            model.addAttribute("totalPageCount",totalPageCount);
            model.addAttribute("totalCount",totalCount);
            model.addAttribute("currentPageNo",currentPageNo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "backend/applist";
    }
}
